/*
		Aim: C program to implement the python list datatype in c using int arrays

*/

#include<stdio.h>
#include<malloc.h>

int *arr,size;

void initial(void)
{
	int i;
	printf("Enter the number of elements: ");
	scanf("%d", &size);
	arr = malloc(size*sizeof(int));
  	for (i=0; i<size; i++) 
	{
    		printf("Enter number %d: ", i+1);
	    	scanf("%d", &arr[i]);
  	}
	printf("\nYour list has been created !!\n");
}

void menu(void)
{
	printf("\n\t***********************************************************\n");
	printf("\t\n Choose an action to perform on the list :\n");
	printf("1. Display the list elements \n");
	printf("2. Append an element to the list\n");
	printf("3. Extend the list\n");
	printf("4. Insert an element at ith position\n");
	printf("5. Remove an element from the list\n");
	printf("6. Pop an element from the list\n");
	printf("7. Get the index of an element\n");
	printf("8. Get the count of the number of occurence of that element\n");
	printf("9. Sort the list.\n");
	printf("10. Reverse the list.\n");
	printf("\n\t***********************************************************\n");
	printf("Enter your choice :");
}

void display(void)
{
	printf("Displaying your list now :\n");
	int i;
	printf("["); 
	for(i=0;i<size-1;i++)
		printf(" %d,",arr[i]);
	printf(" %d]\n",arr[i]);
}

void reverse(void)
{
	int i,l,temp;
	i=0;
	l=size-1;
	while(i<l)
	{
		temp=arr[i];
		arr[i]=arr[l];
		arr[l]=temp;
		i++;l--;
	}
}

void append(int n)
{
	int i=0;
	size++;
	arr=realloc(arr,size*sizeof(int));
	arr[size-1]=n;		
}

void extend()
{
	int i,size1;
	printf("Enter the number of elements: ");
	scanf("%d", &size1);
	size1+=size;
	arr = realloc(arr,size1*sizeof(int));	
	for(i=size;i<size1;i++)
	{
		printf("%d",i);
		printf("Enter number %d: ", i+1);
	    	scanf("%d", &arr[i]);
	}		
	size=size1;
	printf("\nElements have been added to the list !! \n");
}

void insert(int n, int pos)
{
	int l;
	size++;
	arr = realloc(arr,size*sizeof(int));	
	for(l=size-1;l>=pos;l--)
	{
		arr[l+1]=arr[l];
	}
	arr[pos]=n;
	printf("\nelement inserted !! \n");
	display();
}

void rem(int n)
{
	int i,flag=1;
	for(i=0;arr[i]!=n;i++)
	{
		if(i==size-1)
		{	
			printf("element not found\n");
			flag=0;
			break;
		}
	}
	if(flag==1)
	{
		while(i<size)
		{
			arr[i]=arr[i+1];
			i++;
		}
		size--;
		arr = realloc(arr,size*sizeof(int));	
		printf("\nelement deleted !! \n");
		display();
	}
}

int pop(int i)
{
	int j;
	j=arr[i-1];
	i--;
	while(i<size)
	{
		arr[i]=arr[i+1];
		i++;
	}
	size--;
	arr = realloc(arr,size*sizeof(int));
	printf("element popped !! \n");
	display();
	return j;
}

int ind(int n)
{
	int i,flag=0;
	for(i=0;i<size;i++)
	{
		if(arr[i]==n)
		{
			return i+1;
			flag=1;
		}
	}
	if(flag==0)
		return 0;
}

int count(int x)
{
	int i,j=0;
	for(i=0;i<size;i++)
	{
		if(arr[i]==x)
			j++;
	}
	return j;
}

  
void sort(void)  
{  
	int i,j,t;  
	for(i=size-2;i>=0;i--)  
	{  
		for(j=0;j<=i;j++)  
		{  
			if(arr[j]>arr[j+1])  
			{  
				t=arr[j];  
				arr[j]=arr[j+1];  
				arr[j+1]=t;  
			}  
		}  
	}  
}
   
int main(void)
{	
	int ch,n,k;
	char ans='y';
	initial();
do{
	menu();
	scanf("%d",&ch);	
	
	switch(ch)
	{
		case 1:display();
			printf("\nDo u wish to continue <y/n>??\n");
			scanf("%s",&ans);
			break;
		case 2: 
			printf("enter the element to be appended :");
			scanf("%d",&n);
			append(n);
			display();
			printf("\nDo u wish to continue <y/n>??\n");
			scanf("%s",&ans);
			break; 
		case 3:
			printf("enter the new set of elements to be added:\n");
			extend();
			display();
			printf("\nDo u wish to continue <y/n>??\n");
			scanf("%s",&ans);
			break;
		case 4:	
			printf("enter the element to be inserted :");
			scanf("%d",&n);
			printf("enter the position :");
			scanf("%d",&k);
			if(k>size)
			{
				printf("Beyond the array size\n");
				printf("\nDo u wish to continue <y/n>??\n");
				scanf("%s",&ans);
				break;
			}
			k--;
			insert(n,k);
			printf("\nDo u wish to continue <y/n>??\n");
			scanf("%s",&ans);
			break;
		case 5:
			printf("enter the element to be removed :");
			scanf("%d",&n);
			rem(n);
			printf("\nDo u wish to continue <y/n>??\n");
			scanf("%s",&ans);
			break;
		case 6: 
			printf("enter the position of the element to be popped :");
			scanf("%d",&n);
			if(n>size)
			{
				printf("Beyond the array size\n");
				printf("\nDo u wish to continue <y/n>??\n");
				scanf("%s",&ans);
				break;
			}
			k=pop(n);
			printf("element popped out is :%d\n",k);
			printf("\nDo u wish to continue <y/n>??\n");
			scanf("%s",&ans);
			break;
		case 7:
			printf("enter the element to get the index value :");				
			scanf("%d",&n);
			k=ind(n);	
			if(k==0)	
				printf("element not found\n");
			else
				printf("element %d found at index : %d\n",n,k);
			printf("\nDo u wish to continue <y/n>??\n");
			scanf("%s",&ans);
			break;
		case 8: 
			printf("enter the element to be counted :");
			scanf("%d",&n);
			k=count(n);
			printf("the number of occurences of %d in the list is: %d\n",n,k);
			printf("\nDo u wish to continue <y/n>??\n");
			scanf("%s",&ans);
			break;	
		case 9:
			sort();
			printf("\nyour list has been sorted\n");
			display();
			printf("\nDo u wish to continue <y/n>??\n");
			scanf("%s",&ans);
			break;
		case 10:
			reverse();
			printf("\nyour list has been reversed\n");
			display();
			printf("\nDo u wish to continue <y/n>??\n");
			scanf("%s",&ans);
			break;
	}
}while(ans=='y'||ans=='Y');
printf("\nthe program is quiting....\n");
}
